﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using GTA;

namespace GTAHookAdv.Overlay
{

    /// <include file='ProgressBarOverlay.doc' path='IProgressBarOverlay/Type[@name="IProgressBarOverlay"]/*' />
    public class ProgressBarOverlay : ControlOverlay<System.Windows.Forms.ProgressBar>, GTAHookAdv.Overlay.IProgressBarOverlay
    {
        

       

        /// <summary>
        /// Constructor. As all OverlayControls, you must give a reference to your script
        /// </summary>
        /// <param name="owner">simply type "this" from your script</param>
        public ProgressBarOverlay(Script owner)
            : base(owner)
        {


            this.LocationChanged += UpdateGraphicsPos;
            this.SizeChanged += UpdateGraphicsPos;
            this.BackColorChanged += UpdateGraphicsPos; //Because font color depend on it.
            this.OpacityChanged += UpdateGraphicsPos; //Because font color depend on it.
            UpdateGraphicsPos(this, EventArgs.Empty);
            this.Text = String.Empty;
            this.BorderColor = Color.White;

        }

        void UpdateGraphicsPos(object sender, EventArgs e)
        {

            _rectText = new RectangleF(PointToScreen(new PointF(this.Location.X + 4, this.Location.Y + 4)), new SizeF(this.Size.Width - 6, this.Size.Height - 6));
            

           
            FontBuild(_rectText,ref _font);

        }


        /// <summary>
        /// See <see cref="IDisposable.Dispose"/>
        /// </summary>
        public override void Dispose()
        {
            this.LocationChanged -= UpdateGraphicsPos;
            this.SizeChanged -= UpdateGraphicsPos;
            this.BackColorChanged -= UpdateGraphicsPos;
            this.OpacityChanged -= UpdateGraphicsPos;
            base.Dispose();
            if (_font != null)
            {
                _font.Dispose();
                _font = null;
            }
        }

        
        private RectangleF _rectText;
        private GTA.Font _font;


#pragma warning disable 1591 //ignore no documentation comment warning for this
        protected override void Draw()
        {
#pragma warning restore 1591


            

            float valueToPixels = ((float)this.Value / (float)this.Maximum) * (float)this.Size.Width;
            RectangleF clientSize = new RectangleF(PointToScreen(new PointF(this.Location.X + 2, this.Location.Y + 2)), new SizeF(valueToPixels - 4, this.Size.Height - 4));
            DrawRectangle(clientSize, this.ForeColorTransparent,DrawOptions.IgnoreOpacity);
            if (!String.IsNullOrEmpty(this.Text))
                DrawText(this.Text, _rectText, TextAlignment.Center, _font);
        }


        /// <include file='ControlOverlay.doc' path='IControlOverlay/Field[@name="Text"]/*' />
        [Bindable(true), EditorBrowsable(EditorBrowsableState.Always), Browsable(true)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }

        /// <include file='ProgressBarOverlay.doc' path='IProgressBarOverlay/Field[@name="ValuePercent"]/*' />
        public int ValuePercent
        {
            get { return Convert.ToInt32((((float)this.Value) / ((float)(this.Maximum)) * 100f)); }
            set
            {
                if (value < 0 || value > 100) throw new ArgumentOutOfRangeException("ValuePercent must be set from 0 to 100 only. Use Value directly if you want to use bigger range. Received : " + value.ToString());
                this.Minimum = 0;
                this.Maximum = 100;
                this.Value = value;
            }
        }


        #region fields To Route
        /// <include file='ProgressBarOverlay.doc' path='IProgressBarOverlay/Field[@name="Value"]/*' />
        public int Value
        {
            get { return Ctrl.Value; }
            set { Ctrl.Value = value; }
        }

        /// <include file='ProgressBarOverlay.doc' path='IProgressBarOverlay/Field[@name="Minimum"]/*' />
        public int Minimum
        {
            get { return Ctrl.Minimum; }
            set { Ctrl.Minimum = value; }
        }

        /// <include file='ProgressBarOverlay.doc' path='IProgressBarOverlay/Field[@name="Maximum"]/*' />
        public int Maximum
        {
            get { return Ctrl.Maximum; }
            set { Ctrl.Maximum = value; }
        }
        #endregion
    }
}
